
<template>
  <div class="app">
    <div class="venue_wrap df_sb">
      <div class="df_ac">
        <!-- <strong class="f_size18 mr6">合肥市体育馆</strong>
        <el-popover placement="bottom-end" trigger="click">
          <div>111111</div>
          <img
            slot="reference"
            class="pointer"
            style="width: 22px"
            src="@/assets/icon/zhankai.png"
            alt=""
          >
        </el-popover> -->
      </div>
      <el-button
        v-if="meta.indexOf('SaveMineRenovationInfo') > -1"
        v-preventReClick
        :loading="subLoading"
        type="primary"
        @click="onSubmit"
      >{{ translate("saveAndPub") }}</el-button>
    </div>
    <div class="mini_main df mt20">
      <div class="module_wrap">
        <div class="df_sb mb20">
          <strong class="f_size18">{{ translate("myPage") }}</strong>
        </div>
        <div class="module_list">
          <!-- <Container @drop="onDrop"> -->
          <div v-for="item in moduleList" :key="item.code">
            <div
              class="item"
              :class="{ active: activeItem.code === item.code }"
              @click="moduleItemClick(item)"
            >
              <!-- <img class="drag" src="@/assets/icon/drag.png" alt=""> -->
              <template v-if="!item.isEdit">
                <div>
                  <span class="mr6">{{ item.name }}</span>
                  <i
                    v-if="item.iscanRename == '1'"
                    class="el-icon-edit pointer"
                    @click="moduleEditClick(item.code, true)"
                  />
                </div>
                <div class="fade df">
                  <img
                    v-if="
                      item.content.isDisplay === '1' && item.iscanDisplay == '1'
                    "
                    style="width: 22px"
                    class="pointer item_icon mr10"
                    src="@/assets/icon/zhuangxiu_ic_xianshi.png"
                    alt=""
                    @click="isShowClick(item.code, '0')"
                  >
                  <img
                    v-if="
                      item.content.isDisplay === '0' && item.iscanDisplay == '1'
                    "
                    style="width: 22px"
                    class="pointer mr10"
                    src="@/assets/icon/zhuangxiu_ic_yincang_hover.png"
                    alt=""
                    @click="isShowClick(item.code, '1')"
                  >
                  <!-- <img
                      v-if="item.required != '1'"
                      style="width: 22px;"
                      class="item_icon pointer"
                      src="@/assets/icon/zhuangxiu_ic_delete.png"
                      alt=""
                      @click="moduleItemDel(item.code)"
                    > -->
                </div>
              </template>

              <template v-if="item.isEdit">
                <div class="input_wrap flex1 mr10">
                  <el-input
                    v-model="item.content.name"
                    :maxlength="item.maxNameNum"
                  />
                  <span
                    class="text"
                  >{{
                    (item.content.name && item.content.name.length) || 0
                  }}/{{ item.maxNameNum }}</span>
                </div>
                <span
                  v-preventReClick
                  class="pointer primaryColor mr10"
                  @click="moduleNameSave(item.code, false)"
                >{{ translate("save") }}</span>
                <img
                  src="@/assets/icon/zhuangxiu_ic_cancel.png"
                  class="pointer"
                  alt=""
                  @click="moduleEditClick(item.code, false)"
                >
              </template>
            </div>
          </div>
          <!-- </Container> -->
        </div>
      </div>
      <div class="simulator_wrap">
        <div class="simulator">
          <img src="@/icons/png/StatusBar.png" class="StatusBar">
          <div ref="simulator" class="simulator_main">
            <tabHome
              ref="tabHome"
              :glabo-config="miniConfig"
              :config="moduleList"
            />
          </div>
        </div>
      </div>
      <div v-if="moduleList.length > 0" class="option_wrap">
        <Modules
          v-show="activeItem.code === 'M_MYFUNCTION'"
          ref="ModulesRef"
          :config="myConfig"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { applyDrag } from '@/views/application/miniFinish/util/drag'
import Modules from './components-option/modules.vue'
import tabHome from '../home/components-option/tabHome.vue'

import {
  getHomeRenovationInfo,
  updateHomeRenovationInfo
} from '@/api'
export default {
  components: {
    tabHome,
    Modules
  },
  props: {
    config: {
      type: Object,
      default: () => ({})
    },
    meta: {
      type: Array,
      default: () => ([])
    }
  },
  data() {
    return {
      moduleList: [],
      moduleListTemp: [],
      activeItem: {},
      subLoading: false,
      miniConfig: {},

      acticeConfig: {},
      myConfig: {}
    }
  },
  watch: {
    config: {
      handler(newValue) {
        this.miniConfig = newValue
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    this.initHomeData()
  },
  methods: {
    onSubmit() {
      const { moduleList } = this
      moduleList.forEach(item => {
        if (item.code === 'M_MYFUNCTION') { // banner
          item.venueContent = this.$refs.ModulesRef.venueContent
        }
      })
      const param = {
        code: 'T_MINE',
        homeInfos: moduleList
      }
      this.subLoading = true
      updateHomeRenovationInfo({ param }).then(res => {
        this.subLoading = false
        if (res.errorCode !== '000000') return
        this.$store.dispatch('app/resetMiniFlag')
        this.$message.success(this.translate('saveScs'))
        this.$refs.tabHome.returnTop()
      }, () => {
        this.subLoading = false
      })
    },
    initHomeData() {
      getHomeRenovationInfo({ param: { code: 'T_MINE' }}).then(res => {
        if (res.errorCode !== '000000') return
        this.moduleList = [...res.recordList]
        this.moduleListTemp = this.moduleList.map(item => {
          const obj = {
            name: item.name,
            isDisplay: item.isDisplay
            // isDeleted: item.isDeleted
          }
          return obj
        })
        this.activeItem = this.moduleList[1]

        this.moduleList.forEach(item => {
          switch (item.code) {
            case 'M_SHARE':
              this.acticeConfig = item
              break
            case 'M_MYFUNCTION':
              this.myConfig = item
              break
            default:
              break
          }
        })
      })
    },
    compareModule() {
      const { moduleList, moduleListTemp } = this
      let count = 0
      moduleListTemp.forEach((item, i) => {
        if (item.name !== moduleList[i].name) {
          count++
        }
        if (item.isDisplay !== moduleList[i].isDisplay) {
          count++
        }
      })
      this.$store.dispatch('app/compareMyAction', {
        code: 'miniCompareMy',
        flag: count > 0
      })
    },
    moduleItemClick(val) {
      this.activeItem = val
    },
    isShowClick(code, flag) {
      const moduleList = [...this.moduleList]
      const index = moduleList.findIndex(i => i.code === code)
      moduleList[index].content.isDisplay = flag
      moduleList[index].isDisplay = flag
      this.moduleList = moduleList
      this.compareModule()
    },
    moduleEditClick(code, falg) {
      const moduleList = [...this.moduleList]
      const index = moduleList.findIndex(i => i.code === code)
      moduleList[index].isEdit = falg
      moduleList[index].content.name = falg ? moduleList[index].name : ''
      this.moduleList = moduleList
    },
    moduleNameSave(code, falg) {
      const moduleList = [...this.moduleList]
      const index = moduleList.findIndex(i => i.code === code)
      moduleList[index].isEdit = falg
      moduleList[index].name = moduleList[index].content.name
      this.moduleList = moduleList
      this.compareModule()
    },
    onDrop(dropResult) {
      this.moduleList = applyDrag(this.moduleList, dropResult)
    }
  }
}
</script>

<style lang="scss" scoped>
.module_wrap {
  width: 300px;
  height: 100%;
  padding: 20px;
  background-color: #fff;
  font-size: 14px;
  .module_list {
    height: calc(100% - 20px);
    overflow-y: auto;
    &::-webkit-scrollbar {
      display: none;
    }
    width: 260px;
    .item {
      position: relative;
      width: 260px;
      height: 60px;
      padding: 0 20px;
      background: #ffffff;
      border: 1px solid #dfe2e8;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 10px;
      cursor: pointer;
      .item_icon {
        display: none;
        img {
          cursor: pointer;
        }
      }
      &:hover {
        .item_icon {
          display: block;
        }
        .item_icon_none {
          display: none;
        }
      }
      .input_wrap {
        position: relative;
        .text {
          position: absolute;
          top: 11px;
          right: 10px;
          font-size: 12px;
          color: #999999;
        }
        ::v-deep .el-input__inner {
          padding-right: 46px;
        }
      }
      .drag {
        position: absolute;
        width: 16px;
        height: 16px;
        left: 0;
        top: 0;
      }
    }
    .active {
      background: #f4f7fc;
      border: 1px solid #435ebe;
    }
  }
}
.simulator_wrap {
  width: 818px;
  height: 1464px;
  padding: 32px 34px;
  flex: none;
  background-color: #fff;
  position: relative;
  background: url("~@/icons/png/ic_shouji.png");
  background-size: 100% 100%;
  /* Old IE only */
  -moz-transform: scale(0.34);
  -webkit-transform: scale(0.34);
  transform: scale(0.34);
  transform-origin: top left;

  .simulator {
    position: relative;
    width: 100%;
    height: 100%;
    border-radius: 50px;
    overflow: hidden;
    background-color: #f7f7f7;

    .simulator_main {
      width: 100%;
      height: 100%;
      overflow-y: auto;

      &::-webkit-scrollbar {
        display: none;
      }
    }

    .StatusBar {
      height: 88px;
      width: 100%;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 2;
    }
  }
}
.option_wrap {
  background-color: #fff;
  flex: 1;
  margin-left: -500px;
}
.mini_main {
  /* background-color: #f5f6fd; */
  height: calc(100% - 140px);
}
.app {
  height: 100%;
}
</style>
